from marshmallow import Schema, fields, validate
from datetime import datetime

class PrescriptionItemSchema(Schema):
    """处方项目模式"""
    drugId = fields.Str(required=True)
    drugName = fields.Str()
    specification = fields.Str()
    dosage = fields.Str(required=True)
    frequency = fields.Str(required=True)
    quantity = fields.Integer(required=True)
    days = fields.Integer()
    instructions = fields.Str()

class PrescriptionSchema(Schema):
    """处方模式"""
    prescriptionId = fields.Str()
    patientId = fields.Str(required=True)
    doctorId = fields.Str(required=True)
    departmentId = fields.Str(required=True)
    medicalRecordId = fields.Str()  # 关联的医疗记录ID
    visitDate = fields.DateTime(required=True)
    diagnosis = fields.List(fields.Str(), required=True)
    items = fields.List(fields.Nested(PrescriptionItemSchema), required=True)
    totalAmount = fields.Float()
    status = fields.Str(validate=validate.OneOf(['待审核', '已审核', '已发药', '已取消']), dump_default='待审核')
    remarks = fields.Str()
    auditBy = fields.Str()
    auditTime = fields.DateTime()
    dispensedBy = fields.Str()
    dispensedTime = fields.DateTime()
    createdAt = fields.DateTime(dump_default=datetime.utcnow)
    updatedAt = fields.DateTime(dump_default=datetime.utcnow)

class PrescriptionUpdateSchema(Schema):
    """处方更新模式"""
    diagnosis = fields.List(fields.Str())
    items = fields.List(fields.Nested(PrescriptionItemSchema))
    medicalRecordId = fields.Str()  # 关联的医疗记录ID
    status = fields.Str(validate=validate.OneOf(['待审核', '已审核', '已发药', '已取消']))
    remarks = fields.Str()
    auditBy = fields.Str()
    auditTime = fields.DateTime()
    dispensedBy = fields.Str()
    dispensedTime = fields.DateTime()

class PrescriptionQuerySchema(Schema):
    """处方查询模式"""
    patientId = fields.Str()
    doctorId = fields.Str()
    departmentId = fields.Str()
    medicalRecordId = fields.Str()  # 支持按医疗记录ID查询
    status = fields.Str(validate=validate.OneOf(['待审核', '已审核', '已发药', '已取消', 'all']))
    startDate = fields.Date()
    endDate = fields.Date()
    page = fields.Integer(dump_default=1)
    limit = fields.Integer(dump_default=20) 